<?php
namespace Backend\Controllers\Car;

use Backend\Controllers\BaseController;
use Backend\Models\Car\Lottery;
use Backend\Models\Car\ServerType;

/**
 * @RoutePrefix("/car/lottery")
 */
class LotteryController extends BaseController
{
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * @Post(
     *     "/gets"
     * )
     */
    public function getsAction()
    {
        $data = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('pageSize')
        ];
        if ($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->json(['code' => 500, 'msg' => '未获取到当前的页数！']);
        }
        if ($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->json(['code' => 500, 'msg' => '未获取到当前的每页的条数！']);
        }
        $data['size'] = intval($data['size']);
        $data['page'] = intval($data['page']);
        $offset       = intval(($data['page'] - 1) * $data['size']);
        $model        = new Lottery();
        $list         = $model->gets($data['size'], $offset);
        if ($list !== false) {
            return $this->json([
                'code' => 200,
                'msg'  => '获取成功!',
                'list' => $list
            ]);
        }
        return $this->json([
            'code' => 500,
            'msg'  => '获取失败，系统错误~'
        ]);
    }

    /**
     * @Post(
     *     "/getCount"
     * )
     */
    public function getCountAction()
    {
        $cnt = (new Lottery())->getCount();
        if ($cnt === false) {
            return $this->json([
                'code' => 500,
                'msg'  => '获取失败，系统错误~'
            ]);
        }
        return $this->json([
            'code'  => 200,
            'count' => $cnt
        ]);
    }

    /**
     * @Post(
     *     "/remove"
     * )
     */
    public function removeAction()
    {
        $id = $this->request->getPost('id');
        if ($id == '' || !is_numeric($id)) {
            return $this->json([
                'code' => 501,
                'msg'  => '参数丢失～'
            ]);
        }
        $model = new Lottery();
        $ret   = $model->remove($id);
        if ($ret !== false) {
            return $this->json([
                'code' => 200,
                'msg'  => '删除成功'
            ]);
        }
        return $this->json([
            'code' => 501,
            'msg'  => '系统错误，请稍候再试～'
        ]);
    }
}
